<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>perlobj - Perl object reference - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/perlobj">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlobj.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/perlobj">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/perlobj">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/perlobj">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/perlobj">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/perlobj">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/perlobj">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/perlobj">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/perlobj">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/perlobj">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/perlobj">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/perlobj">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/perlobj">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/perlobj">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/perlobj">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/perlobj">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/perlobj">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/perlobj">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/perlobj">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/perlobj">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/perlobj">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/perlobj">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/perlobj">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/perlobj">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/perlobj">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/perlobj">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/perlobj">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/perlobj">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/perlobj">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/perlobj">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/perlobj">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/perlobj">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/perlobj">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/perlobj">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/perlobj">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/perlobj">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/perlobj">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/perlobj">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/perlobj">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/perlobj">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/perlobj">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/perlobj">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/perlobj">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/perlobj">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/perlobj">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/perlobj">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/perlobj">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/perlobj">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/perlobj">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/perlobj">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/perlobj">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/perlobj">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/perlobj">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/perlobj">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/perlobj">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/perlobj">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/perlobj">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/perlobj">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/perlobj">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/perlobj">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/perlobj">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/perlobj">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/perlobj">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/perlobj">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/perlobj">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/perlobj">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/perlobj">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/perlobj">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/perlobj">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/perlobj">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/perlobj">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/perlobj">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/perlobj">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/perlobj">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/perlobj">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/perlobj">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/perlobj">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/perlobj">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/perlobj">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/perlobj">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/perlobj">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlobj.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/perlobj">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/perlobj">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/perlobj">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/perlobj">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/perlobj">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/perlobj">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/perlobj">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/perlobj">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/perlobj">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/perlobj">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/perlobj">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/perlobj">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/perlobj">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/perlobj">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/perlobj">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/perlobj">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/perlobj">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/perlobj">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/perlobj">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/perlobj">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/perlobj">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/perlobj">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/perlobj">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/perlobj">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/perlobj">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/perlobj">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/perlobj">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/perlobj">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/perlobj">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/perlobj">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/perlobj">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/perlobj">5.35.1</a>
          <a class="dropdown-item active" href="perlobj.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlobj.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="perlobj.html">perlobj</a>
          <div id="more">
            (<a href="perlobj.txt">source</a>,
            <a href="https://metacpan.org/pod/perlobj">CPAN</a>)
          </div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="perlobj.html#NAME">NAME  </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#DESCRIPTION">DESCRIPTION</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlobj.html#An-Object-is-Simply-a-Data-Structure">An Object is Simply a Data Structure    </a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Objects-Are-Blessed;-Variables-Are-Not">Objects Are Blessed; Variables Are Not</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#A-Class-is-Simply-a-Package">A Class is Simply a Package    </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#A-Method-is-Simply-a-Subroutine">A Method is Simply a Subroutine </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Method-Invocation">Method Invocation    </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Inheritance">Inheritance </a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlobj.html#How-SUPER-is-Resolved">How SUPER is Resolved </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Multiple-Inheritance">Multiple Inheritance </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Method-Resolution-Order">Method Resolution Order  </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Method-Resolution-Caching">Method Resolution Caching</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Writing-Constructors">Writing Constructors </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Attributes">Attributes </a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Writing-Accessors">Writing Accessors </a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#An-Aside-About-Smarter-and-Safer-Code">An Aside About Smarter and Safer Code</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Method-Call-Variations">Method Call Variations </a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Method-Names-with-a-Fully-Qualified-Name">Method Names with a Fully Qualified Name</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Method-Names-as-Strings">Method Names as Strings</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Class-Names-as-Strings">Class Names as Strings</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Subroutine-References-as-Methods">Subroutine References as Methods</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Dereferencing-Method-Call">Dereferencing Method Call</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Method-Calls-on-Filehandles">Method Calls on Filehandles</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Invoking-Class-Methods">Invoking Class Methods </a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Indirect-Object-Syntax">Indirect Object Syntax </a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#bless,-blessed,-and-ref">bless, blessed, and ref</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#The-UNIVERSAL-Class">The UNIVERSAL Class </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#AUTOLOAD">AUTOLOAD </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Destructors">Destructors  </a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Global-Destruction">Global Destruction</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Non-Hash-Objects">Non-Hash Objects</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Inside-Out-objects">Inside-Out objects</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#Pseudo-hashes">Pseudo-hashes</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlobj.html#SEE-ALSO">SEE ALSO</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="perlobj.html#NAME">#</a>NAME  </h1>

<p>perlobj - Perl object reference</p>

<h1 id="DESCRIPTION"><a class="permalink" href="perlobj.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p>This document provides a reference for Perl&#39;s object orientation features. If you&#39;re looking for an introduction to object-oriented programming in Perl, please see <a href="perlootut.html">perlootut</a>.</p>

<p>In order to understand Perl objects, you first need to understand references in Perl. See <a href="perlreftut.html">perlreftut</a> for details.</p>

<p>This document describes all of Perl&#39;s object-oriented (OO) features from the ground up. If you&#39;re just looking to write some object-oriented code of your own, you are probably better served by using one of the object systems from CPAN described in <a href="perlootut.html">perlootut</a>.</p>

<p>If you&#39;re looking to write your own object system, or you need to maintain code which implements objects from scratch then this document will help you understand exactly how Perl does object orientation.</p>

<p>There are a few basic principles which define object oriented Perl:</p>

<ol>

<li><p>An object is simply a data structure that knows to which class it belongs.</p>

</li>
<li><p>A class is simply a package. A class provides methods that expect to operate on objects.</p>

</li>
<li><p>A method is simply a subroutine that expects a reference to an object (or a package name, for class methods) as the first argument.</p>

</li>
</ol>

<p>Let&#39;s look at each of these principles in depth.</p>

<h2 id="An-Object-is-Simply-a-Data-Structure"><a class="permalink" href="perlobj.html#An-Object-is-Simply-a-Data-Structure">#</a><a id="An"></a>An Object is Simply a Data Structure    </h2>

<p>Unlike many other languages which support object orientation, Perl does not provide any special syntax for constructing an object. Objects are merely Perl data structures (hashes, arrays, scalars, filehandles, etc.) that have been explicitly associated with a particular class.</p>

<p>That explicit association is created by the built-in <code>bless</code> function, which is typically used within the <i>constructor</i> subroutine of the class.</p>

<p>Here is a simple constructor:</p>

<pre><code>package File;

sub new {
    my $class = shift;

    return bless {}, $class;
}</code></pre>

<p>The name <code>new</code> isn&#39;t special. We could name our constructor something else:</p>

<pre><code>package File;

sub load {
    my $class = shift;

    return bless {}, $class;
}</code></pre>

<p>The modern convention for OO modules is to always use <code>new</code> as the name for the constructor, but there is no requirement to do so. Any subroutine that blesses a data structure into a class is a valid constructor in Perl.</p>

<p>In the previous examples, the <code>{}</code> code creates a reference to an empty anonymous hash. The <code>bless</code> function then takes that reference and associates the hash with the class in <code>$class</code>. In the simplest case, the <code>$class</code> variable will end up containing the string &quot;File&quot;.</p>

<p>We can also use a variable to store a reference to the data structure that is being blessed as our object:</p>

<pre><code>sub new {
    my $class = shift;

    my $self = {};
    bless $self, $class;

    return $self;
}</code></pre>

<p>Once we&#39;ve blessed the hash referred to by <code>$self</code> we can start calling methods on it. This is useful if you want to put object initialization in its own separate method:</p>

<pre><code>sub new {
    my $class = shift;

    my $self = {};
    bless $self, $class;

    $self-&gt;_initialize();

    return $self;
}</code></pre>

<p>Since the object is also a hash, you can treat it as one, using it to store data associated with the object. Typically, code inside the class can treat the hash as an accessible data structure, while code outside the class should always treat the object as opaque. This is called <b>encapsulation</b>. Encapsulation means that the user of an object does not have to know how it is implemented. The user simply calls documented methods on the object.</p>

<p>Note, however, that (unlike most other OO languages) Perl does not ensure or enforce encapsulation in any way. If you want objects to actually <i>be</i> opaque you need to arrange for that yourself. This can be done in a variety of ways, including using <a href="perlobj.html#Inside-Out-objects">&quot;Inside-Out objects&quot;</a> or modules from CPAN.</p>

<h3 id="Objects-Are-Blessed;-Variables-Are-Not"><a class="permalink" href="perlobj.html#Objects-Are-Blessed;-Variables-Are-Not">#</a><a id="Objects"></a><a id="Objects-Are-Blessed-Variables-Are-Not"></a>Objects Are Blessed; Variables Are Not</h3>

<p>When we bless something, we are not blessing the variable which contains a reference to that thing, nor are we blessing the reference that the variable stores; we are blessing the thing that the variable refers to (sometimes known as the <i>referent</i>). This is best demonstrated with this code:</p>

<pre><code>use Scalar::Util &#39;blessed&#39;;

my $foo = {};
my $bar = $foo;

bless $foo, &#39;Class&#39;;
print blessed( $bar ) // &#39;not blessed&#39;;    # prints &quot;Class&quot;

$bar = &quot;some other value&quot;;
print blessed( $bar ) // &#39;not blessed&#39;;    # prints &quot;not blessed&quot;</code></pre>

<p>When we call <code>bless</code> on a variable, we are actually blessing the underlying data structure that the variable refers to. We are not blessing the reference itself, nor the variable that contains that reference. That&#39;s why the second call to <code>blessed( $bar )</code> returns false. At that point <code>$bar</code> is no longer storing a reference to an object.</p>

<p>You will sometimes see older books or documentation mention &quot;blessing a reference&quot; or describe an object as a &quot;blessed reference&quot;, but this is incorrect. It isn&#39;t the reference that is blessed as an object; it&#39;s the thing the reference refers to (i.e. the referent).</p>

<h2 id="A-Class-is-Simply-a-Package"><a class="permalink" href="perlobj.html#A-Class-is-Simply-a-Package">#</a><a id="A"></a>A Class is Simply a Package    </h2>

<p>Perl does not provide any special syntax for class definitions. A package is simply a namespace containing variables and subroutines. The only difference is that in a class, the subroutines may expect a reference to an object or the name of a class as the first argument. This is purely a matter of convention, so a class may contain both methods and subroutines which <i>don&#39;t</i> operate on an object or class.</p>

<p>Each package contains a special array called <code>@ISA</code>. The <code>@ISA</code> array contains a list of that class&#39;s parent classes, if any. This array is examined when Perl does method resolution, which we will cover later.</p>

<p>Calling methods from a package means it must be loaded, of course, so you will often want to load a module and add it to <code>@ISA</code> at the same time. You can do so in a single step using the <a href="parent.html">parent</a> pragma. (In older code you may encounter the <a href="base.html">base</a> pragma, which is nowadays discouraged except when you have to work with the equally discouraged <a href="fields.html">fields</a> pragma.)</p>

<p>However the parent classes are set, the package&#39;s <code>@ISA</code> variable will contain a list of those parents. This is simply a list of scalars, each of which is a string that corresponds to a package name.</p>

<p>All classes inherit from the <a href="UNIVERSAL.html">UNIVERSAL</a> class implicitly. The <a href="UNIVERSAL.html">UNIVERSAL</a> class is implemented by the Perl core, and provides several default methods, such as <code>isa()</code>, <code>can()</code>, and <code>VERSION()</code>. The <code>UNIVERSAL</code> class will <i>never</i> appear in a package&#39;s <code>@ISA</code> variable.</p>

<p>Perl <i>only</i> provides method inheritance as a built-in feature. Attribute inheritance is left up the class to implement. See the <a href="perlobj.html#Writing-Accessors">&quot;Writing Accessors&quot;</a> section for details.</p>

<h2 id="A-Method-is-Simply-a-Subroutine"><a class="permalink" href="perlobj.html#A-Method-is-Simply-a-Subroutine">#</a><a id="A1"></a>A Method is Simply a Subroutine </h2>

<p>Perl does not provide any special syntax for defining a method. A method is simply a regular subroutine, and is declared with <code>sub</code>. What makes a method special is that it expects to receive either an object or a class name as its first argument.</p>

<p>Perl <i>does</i> provide special syntax for method invocation, the <code>-&gt;</code> operator. We will cover this in more detail later.</p>

<p>Most methods you write will expect to operate on objects:</p>

<pre><code>sub save {
    my $self = shift;

    open my $fh, &#39;&gt;&#39;, $self-&gt;path() or die $!;
    print {$fh} $self-&gt;data()       or die $!;
    close $fh                       or die $!;
}</code></pre>

<h2 id="Method-Invocation"><a class="permalink" href="perlobj.html#Method-Invocation">#</a><a id="Method"></a>Method Invocation    </h2>

<p>Calling a method on an object is written as <code>$object-&gt;method</code>.</p>

<p>The left hand side of the method invocation (or arrow) operator is the object (or class name), and the right hand side is the method name.</p>

<pre><code>my $pod = File-&gt;new( &#39;perlobj.pod&#39;, $data );
$pod-&gt;save();</code></pre>

<p>The <code>-&gt;</code> syntax is also used when dereferencing a reference. It looks like the same operator, but these are two different operations.</p>

<p>When you call a method, the thing on the left side of the arrow is passed as the first argument to the method. That means when we call <code>Critter-&gt;new()</code>, the <code>new()</code> method receives the string <code>&quot;Critter&quot;</code> as its first argument. When we call <code>$fred-&gt;speak()</code>, the <code>$fred</code> variable is passed as the first argument to <code>speak()</code>.</p>

<p>Just as with any Perl subroutine, all of the arguments passed in <code>@_</code> are aliases to the original argument. This includes the object itself. If you assign directly to <code>$_[0]</code> you will change the contents of the variable that holds the reference to the object. We recommend that you don&#39;t do this unless you know exactly what you&#39;re doing.</p>

<p>Perl knows what package the method is in by looking at the left side of the arrow. If the left hand side is a package name, it looks for the method in that package. If the left hand side is an object, then Perl looks for the method in the package that the object has been blessed into.</p>

<p>If the left hand side is neither a package name nor an object, then the method call will cause an error, but see the section on <a href="perlobj.html#Method-Call-Variations">&quot;Method Call Variations&quot;</a> for more nuances.</p>

<h2 id="Inheritance"><a class="permalink" href="perlobj.html#Inheritance">#</a>Inheritance </h2>

<p>We already talked about the special <code>@ISA</code> array and the <a href="parent.html">parent</a> pragma.</p>

<p>When a class inherits from another class, any methods defined in the parent class are available to the child class. If you attempt to call a method on an object that isn&#39;t defined in its own class, Perl will also look for that method in any parent classes it may have.</p>

<pre><code>package File::MP3;
use parent &#39;File&#39;;    # sets @File::MP3::ISA = (&#39;File&#39;);

my $mp3 = File::MP3-&gt;new( &#39;Andvari.mp3&#39;, $data );
$mp3-&gt;save();</code></pre>

<p>Since we didn&#39;t define a <code>save()</code> method in the <code>File::MP3</code> class, Perl will look at the <code>File::MP3</code> class&#39;s parent classes to find the <code>save()</code> method. If Perl cannot find a <code>save()</code> method anywhere in the inheritance hierarchy, it will die.</p>

<p>In this case, it finds a <code>save()</code> method in the <code>File</code> class. Note that the object passed to <code>save()</code> in this case is still a <code>File::MP3</code> object, even though the method is found in the <code>File</code> class.</p>

<p>We can override a parent&#39;s method in a child class. When we do so, we can still call the parent class&#39;s method with the <code>SUPER</code> pseudo-class.</p>

<pre><code>sub save {
    my $self = shift;

    say &#39;Prepare to rock&#39;;
    $self-&gt;SUPER::save();
}</code></pre>

<p>The <code>SUPER</code> modifier can <i>only</i> be used for method calls. You can&#39;t use it for regular subroutine calls or class methods:</p>

<pre><code>SUPER::save($thing);     # FAIL: looks for save() sub in package SUPER

SUPER-&gt;save($thing);     # FAIL: looks for save() method in class
                         #       SUPER

$thing-&gt;SUPER::save();   # Okay: looks for save() method in parent
                         #       classes</code></pre>

<h3 id="How-SUPER-is-Resolved"><a class="permalink" href="perlobj.html#How-SUPER-is-Resolved">#</a><a id="How"></a>How SUPER is Resolved </h3>

<p>The <code>SUPER</code> pseudo-class is resolved from the package where the call is made. It is <i>not</i> resolved based on the object&#39;s class. This is important, because it lets methods at different levels within a deep inheritance hierarchy each correctly call their respective parent methods.</p>

<pre><code>package A;

sub new {
    return bless {}, shift;
}

sub speak {
    my $self = shift;

    say &#39;A&#39;;
}

package B;

use parent -norequire, &#39;A&#39;;

sub speak {
    my $self = shift;

    $self-&gt;SUPER::speak();

    say &#39;B&#39;;
}

package C;

use parent -norequire, &#39;B&#39;;

sub speak {
    my $self = shift;

    $self-&gt;SUPER::speak();

    say &#39;C&#39;;
}

my $c = C-&gt;new();
$c-&gt;speak();</code></pre>

<p>In this example, we will get the following output:</p>

<pre><code class="plaintext">A
B
C</code></pre>

<p>This demonstrates how <code>SUPER</code> is resolved. Even though the object is blessed into the <code>C</code> class, the <code>speak()</code> method in the <code>B</code> class can still call <code>SUPER::speak()</code> and expect it to correctly look in the parent class of <code>B</code> (i.e the class the method call is in), not in the parent class of <code>C</code> (i.e. the class the object belongs to).</p>

<p>There are rare cases where this package-based resolution can be a problem. If you copy a subroutine from one package to another, <code>SUPER</code> resolution will be done based on the original package.</p>

<h3 id="Multiple-Inheritance"><a class="permalink" href="perlobj.html#Multiple-Inheritance">#</a><a id="Multiple"></a>Multiple Inheritance </h3>

<p>Multiple inheritance often indicates a design problem, but Perl always gives you enough rope to hang yourself with if you ask for it.</p>

<p>To declare multiple parents, you simply need to pass multiple class names to <code>use parent</code>:</p>

<pre><code>package MultiChild;

use parent &#39;Parent1&#39;, &#39;Parent2&#39;;</code></pre>

<h3 id="Method-Resolution-Order"><a class="permalink" href="perlobj.html#Method-Resolution-Order">#</a><a id="Method1"></a>Method Resolution Order  </h3>

<p>Method resolution order only matters in the case of multiple inheritance. In the case of single inheritance, Perl simply looks up the inheritance chain to find a method:</p>

<pre><code class="plaintext">Grandparent
  |
Parent
  |
Child</code></pre>

<p>If we call a method on a <code>Child</code> object and that method is not defined in the <code>Child</code> class, Perl will look for that method in the <code>Parent</code> class and then, if necessary, in the <code>Grandparent</code> class.</p>

<p>If Perl cannot find the method in any of these classes, it will die with an error message.</p>

<p>When a class has multiple parents, the method lookup order becomes more complicated.</p>

<p>By default, Perl does a depth-first left-to-right search for a method. That means it starts with the first parent in the <code>@ISA</code> array, and then searches all of its parents, grandparents, etc. If it fails to find the method, it then goes to the next parent in the original class&#39;s <code>@ISA</code> array and searches from there.</p>

<pre><code class="plaintext">          SharedGreatGrandParent
          /                    \
PaternalGrandparent       MaternalGrandparent
          \                    /
           Father        Mother
                 \      /
                  Child</code></pre>

<p>So given the diagram above, Perl will search <code>Child</code>, <code>Father</code>, <code>PaternalGrandparent</code>, <code>SharedGreatGrandParent</code>, <code>Mother</code>, and finally <code>MaternalGrandparent</code>. This may be a problem because now we&#39;re looking in <code>SharedGreatGrandParent</code> <i>before</i> we&#39;ve checked all its derived classes (i.e. before we tried <code>Mother</code> and <code>MaternalGrandparent</code>).</p>

<p>It is possible to ask for a different method resolution order with the <a href="mro.html">mro</a> pragma.</p>

<pre><code>package Child;

use mro &#39;c3&#39;;
use parent &#39;Father&#39;, &#39;Mother&#39;;</code></pre>

<p>This pragma lets you switch to the &quot;C3&quot; resolution order. In simple terms, &quot;C3&quot; order ensures that shared parent classes are never searched before child classes, so Perl will now search: <code>Child</code>, <code>Father</code>, <code>PaternalGrandparent</code>, <code>Mother</code> <code>MaternalGrandparent</code>, and finally <code>SharedGreatGrandParent</code>. Note however that this is not &quot;breadth-first&quot; searching: All the <code>Father</code> ancestors (except the common ancestor) are searched before any of the <code>Mother</code> ancestors are considered.</p>

<p>The C3 order also lets you call methods in sibling classes with the <code>next</code> pseudo-class. See the <a href="mro.html">mro</a> documentation for more details on this feature.</p>

<h3 id="Method-Resolution-Caching"><a class="permalink" href="perlobj.html#Method-Resolution-Caching">#</a><a id="Method2"></a>Method Resolution Caching</h3>

<p>When Perl searches for a method, it caches the lookup so that future calls to the method do not need to search for it again. Changing a class&#39;s parent class or adding subroutines to a class will invalidate the cache for that class.</p>

<p>The <a href="mro.html">mro</a> pragma provides some functions for manipulating the method cache directly.</p>

<h2 id="Writing-Constructors"><a class="permalink" href="perlobj.html#Writing-Constructors">#</a><a id="Writing"></a>Writing Constructors </h2>

<p>As we mentioned earlier, Perl provides no special constructor syntax. This means that a class must implement its own constructor. A constructor is simply a class method that returns a reference to a new object.</p>

<p>The constructor can also accept additional parameters that define the object. Let&#39;s write a real constructor for the <code>File</code> class we used earlier:</p>

<pre><code>package File;

sub new {
    my $class = shift;
    my ( $path, $data ) = @_;

    my $self = bless {
        path =&gt; $path,
        data =&gt; $data,
    }, $class;

    return $self;
}</code></pre>

<p>As you can see, we&#39;ve stored the path and file data in the object itself. Remember, under the hood, this object is still just a hash. Later, we&#39;ll write accessors to manipulate this data.</p>

<p>For our <code>File::MP3</code> class, we can check to make sure that the path we&#39;re given ends with &quot;.mp3&quot;:</p>

<pre><code>package File::MP3;

sub new {
    my $class = shift;
    my ( $path, $data ) = @_;

    die &quot;You cannot create a File::MP3 without an mp3 extension\n&quot;
        unless $path =~ /\.mp3\z/;

    return $class-&gt;SUPER::new(@_);
}</code></pre>

<p>This constructor lets its parent class do the actual object construction.</p>

<h2 id="Attributes"><a class="permalink" href="perlobj.html#Attributes">#</a>Attributes </h2>

<p>An attribute is a piece of data belonging to a particular object. Unlike most object-oriented languages, Perl provides no special syntax or support for declaring and manipulating attributes.</p>

<p>Attributes are often stored in the object itself. For example, if the object is an anonymous hash, we can store the attribute values in the hash using the attribute name as the key.</p>

<p>While it&#39;s possible to refer directly to these hash keys outside of the class, it&#39;s considered a best practice to wrap all access to the attribute with accessor methods.</p>

<p>This has several advantages. Accessors make it easier to change the implementation of an object later while still preserving the original API.</p>

<p>An accessor lets you add additional code around attribute access. For example, you could apply a default to an attribute that wasn&#39;t set in the constructor, or you could validate that a new value for the attribute is acceptable.</p>

<p>Finally, using accessors makes inheritance much simpler. Subclasses can use the accessors rather than having to know how a parent class is implemented internally.</p>

<h3 id="Writing-Accessors"><a class="permalink" href="perlobj.html#Writing-Accessors">#</a><a id="Writing1"></a>Writing Accessors </h3>

<p>As with constructors, Perl provides no special accessor declaration syntax, so classes must provide explicitly written accessor methods. There are two common types of accessors, read-only and read-write.</p>

<p>A simple read-only accessor simply gets the value of a single attribute:</p>

<pre><code>sub path {
    my $self = shift;

    return $self-&gt;{path};
}</code></pre>

<p>A read-write accessor will allow the caller to set the value as well as get it:</p>

<pre><code>sub path {
    my $self = shift;

    if (@_) {
        $self-&gt;{path} = shift;
    }

    return $self-&gt;{path};
}</code></pre>

<h2 id="An-Aside-About-Smarter-and-Safer-Code"><a class="permalink" href="perlobj.html#An-Aside-About-Smarter-and-Safer-Code">#</a><a id="An1"></a>An Aside About Smarter and Safer Code</h2>

<p>Our constructor and accessors are not very smart. They don&#39;t check that a <code>$path</code> is defined, nor do they check that a <code>$path</code> is a valid filesystem path.</p>

<p>Doing these checks by hand can quickly become tedious. Writing a bunch of accessors by hand is also incredibly tedious. There are a lot of modules on CPAN that can help you write safer and more concise code, including the modules we recommend in <a href="perlootut.html">perlootut</a>.</p>

<h2 id="Method-Call-Variations"><a class="permalink" href="perlobj.html#Method-Call-Variations">#</a><a id="Method3"></a>Method Call Variations </h2>

<p>Perl supports several other ways to call methods besides the <code>$object-&gt;method()</code> usage we&#39;ve seen so far.</p>

<h3 id="Method-Names-with-a-Fully-Qualified-Name"><a class="permalink" href="perlobj.html#Method-Names-with-a-Fully-Qualified-Name">#</a><a id="Method4"></a>Method Names with a Fully Qualified Name</h3>

<p>Perl allows you to call methods using their fully qualified name (the package and method name):</p>

<pre><code>my $mp3 = File::MP3-&gt;new( &#39;Regin.mp3&#39;, $data );
$mp3-&gt;File::save();</code></pre>

<p>When you call a fully qualified method name like <code>File::save</code>, the method resolution search for the <code>save</code> method starts in the <code>File</code> class, skipping any <code>save</code> method the <code>File::MP3</code> class may have defined. It still searches the <code>File</code> class&#39;s parents if necessary.</p>

<p>While this feature is most commonly used to explicitly call methods inherited from an ancestor class, there is no technical restriction that enforces this:</p>

<pre><code>my $obj = Tree-&gt;new();
$obj-&gt;Dog::bark();</code></pre>

<p>This calls the <code>bark</code> method from class <code>Dog</code> on an object of class <code>Tree</code>, even if the two classes are completely unrelated. Use this with great care.</p>

<p>The <code>SUPER</code> pseudo-class that was described earlier is <i>not</i> the same as calling a method with a fully-qualified name. See the earlier <a href="perlobj.html#Inheritance">&quot;Inheritance&quot;</a> section for details.</p>

<h3 id="Method-Names-as-Strings"><a class="permalink" href="perlobj.html#Method-Names-as-Strings">#</a><a id="Method5"></a>Method Names as Strings</h3>

<p>Perl lets you use a scalar variable containing a string as a method name:</p>

<pre><code>my $file = File-&gt;new( $path, $data );

my $method = &#39;save&#39;;
$file-&gt;$method();</code></pre>

<p>This works exactly like calling <code>$file-&gt;save()</code>. This can be very useful for writing dynamic code. For example, it allows you to pass a method name to be called as a parameter to another method.</p>

<h3 id="Class-Names-as-Strings"><a class="permalink" href="perlobj.html#Class-Names-as-Strings">#</a><a id="Class"></a>Class Names as Strings</h3>

<p>Perl also lets you use a scalar containing a string as a class name:</p>

<pre><code>my $class = &#39;File&#39;;

my $file = $class-&gt;new( $path, $data );</code></pre>

<p>Again, this allows for very dynamic code.</p>

<h3 id="Subroutine-References-as-Methods"><a class="permalink" href="perlobj.html#Subroutine-References-as-Methods">#</a><a id="Subroutine"></a>Subroutine References as Methods</h3>

<p>You can also use a subroutine reference as a method:</p>

<pre><code>my $sub = sub {
    my $self = shift;

    $self-&gt;save();
};

$file-&gt;$sub();</code></pre>

<p>This is exactly equivalent to writing <code>$sub-&gt;($file)</code>. You may see this idiom in the wild combined with a call to <code>can</code>:</p>

<pre><code>if ( my $meth = $object-&gt;can(&#39;foo&#39;) ) {
    $object-&gt;$meth();
}</code></pre>

<h3 id="Dereferencing-Method-Call"><a class="permalink" href="perlobj.html#Dereferencing-Method-Call">#</a><a id="Dereferencing"></a>Dereferencing Method Call</h3>

<p>Perl also lets you use a dereferenced scalar reference in a method call. That&#39;s a mouthful, so let&#39;s look at some code:</p>

<pre><code>$file-&gt;${ \&#39;save&#39; };
$file-&gt;${ returns_scalar_ref() };
$file-&gt;${ \( returns_scalar() ) };
$file-&gt;${ returns_ref_to_sub_ref() };</code></pre>

<p>This works if the dereference produces a string <i>or</i> a subroutine reference.</p>

<h3 id="Method-Calls-on-Filehandles"><a class="permalink" href="perlobj.html#Method-Calls-on-Filehandles">#</a><a id="Method6"></a>Method Calls on Filehandles</h3>

<p>Under the hood, Perl filehandles are instances of the <code>IO::Handle</code> or <code>IO::File</code> class. Once you have an open filehandle, you can call methods on it. Additionally, you can call methods on the <code>STDIN</code>, <code>STDOUT</code>, and <code>STDERR</code> filehandles.</p>

<pre><code>open my $fh, &#39;&gt;&#39;, &#39;path/to/file&#39;;
$fh-&gt;autoflush();
$fh-&gt;print(&#39;content&#39;);

STDOUT-&gt;autoflush();</code></pre>

<h2 id="Invoking-Class-Methods"><a class="permalink" href="perlobj.html#Invoking-Class-Methods">#</a><a id="Invoking"></a>Invoking Class Methods </h2>

<p>Because Perl allows you to use barewords for package names and subroutine names, it sometimes interprets a bareword&#39;s meaning incorrectly. For example, the construct <code>Class-&gt;new()</code> can be interpreted as either <code>&#39;Class&#39;-&gt;new()</code> or <code>Class()-&gt;new()</code>. In English, that second interpretation reads as &quot;call a subroutine named Class(), then call new() as a method on the return value of Class()&quot;. If there is a subroutine named <code>Class()</code> in the current namespace, Perl will always interpret <code>Class-&gt;new()</code> as the second alternative: a call to <code>new()</code> on the object returned by a call to <code>Class()</code></p>

<p>You can force Perl to use the first interpretation (i.e. as a method call on the class named &quot;Class&quot;) in two ways. First, you can append a <code>::</code> to the class name:</p>

<pre><code>Class::-&gt;new()</code></pre>

<p>Perl will always interpret this as a method call.</p>

<p>Alternatively, you can quote the class name:</p>

<pre><code>&#39;Class&#39;-&gt;new()</code></pre>

<p>Of course, if the class name is in a scalar Perl will do the right thing as well:</p>

<pre><code>my $class = &#39;Class&#39;;
$class-&gt;new();</code></pre>

<h3 id="Indirect-Object-Syntax"><a class="permalink" href="perlobj.html#Indirect-Object-Syntax">#</a><a id="Indirect"></a>Indirect Object Syntax </h3>

<p><b>Outside of the file handle case, use of this syntax is discouraged as it can confuse the Perl interpreter. See below for more details.</b></p>

<p>Perl supports another method invocation syntax called &quot;indirect object&quot; notation. This syntax is called &quot;indirect&quot; because the method comes before the object it is being invoked on.</p>

<p>This syntax can be used with any class or object method:</p>

<pre><code>my $file = new File $path, $data;
save $file;</code></pre>

<p>We recommend that you avoid this syntax, for several reasons.</p>

<p>First, it can be confusing to read. In the above example, it&#39;s not clear if <code>save</code> is a method provided by the <code>File</code> class or simply a subroutine that expects a file object as its first argument.</p>

<p>When used with class methods, the problem is even worse. Because Perl allows subroutine names to be written as barewords, Perl has to guess whether the bareword after the method is a class name or subroutine name. In other words, Perl can resolve the syntax as either <code>File-&gt;new( $path, $data )</code> <b>or</b> <code>new( File( $path, $data ) )</code>.</p>

<p>To parse this code, Perl uses a heuristic based on what package names it has seen, what subroutines exist in the current package, what barewords it has previously seen, and other input. Needless to say, heuristics can produce very surprising results!</p>

<p>Older documentation (and some CPAN modules) encouraged this syntax, particularly for constructors, so you may still find it in the wild. However, we encourage you to avoid using it in new code.</p>

<p>You can force Perl to interpret the bareword as a class name by appending &quot;::&quot; to it, like we saw earlier:</p>

<pre><code>my $file = new File:: $path, $data;</code></pre>

<h2 id="bless,-blessed,-and-ref"><a class="permalink" href="perlobj.html#bless,-blessed,-and-ref">#</a><a id="bless"></a><a id="bless-blessed-and-ref"></a><code>bless</code>, <code>blessed</code>, and <code>ref</code></h2>

<p>As we saw earlier, an object is simply a data structure that has been blessed into a class via the <code>bless</code> function. The <code>bless</code> function can take either one or two arguments:</p>

<pre><code>my $object = bless {}, $class;
my $object = bless {};</code></pre>

<p>In the first form, the anonymous hash is being blessed into the class in <code>$class</code>. In the second form, the anonymous hash is blessed into the current package.</p>

<p>The second form is strongly discouraged, because it breaks the ability of a subclass to reuse the parent&#39;s constructor, but you may still run across it in existing code.</p>

<p>If you want to know whether a particular scalar refers to an object, you can use the <code>blessed</code> function exported by <a href="./Scalar::Util.html">Scalar::Util</a>, which is shipped with the Perl core.</p>

<pre><code>use Scalar::Util &#39;blessed&#39;;

if ( defined blessed($thing) ) { ... }</code></pre>

<p>If <code>$thing</code> refers to an object, then this function returns the name of the package the object has been blessed into. If <code>$thing</code> doesn&#39;t contain a reference to a blessed object, the <code>blessed</code> function returns <code>undef</code>.</p>

<p>Note that <code>blessed($thing)</code> will also return false if <code>$thing</code> has been blessed into a class named &quot;0&quot;. This is a possible, but quite pathological. Don&#39;t create a class named &quot;0&quot; unless you know what you&#39;re doing.</p>

<p>Similarly, Perl&#39;s built-in <code>ref</code> function treats a reference to a blessed object specially. If you call <code>ref($thing)</code> and <code>$thing</code> holds a reference to an object, it will return the name of the class that the object has been blessed into.</p>

<p>If you simply want to check that a variable contains an object reference, we recommend that you use <code>defined blessed($object)</code>, since <code>ref</code> returns true values for all references, not just objects.</p>

<h2 id="The-UNIVERSAL-Class"><a class="permalink" href="perlobj.html#The-UNIVERSAL-Class">#</a><a id="The"></a>The UNIVERSAL Class </h2>

<p>All classes automatically inherit from the <a href="UNIVERSAL.html">UNIVERSAL</a> class, which is built-in to the Perl core. This class provides a number of methods, all of which can be called on either a class or an object. You can also choose to override some of these methods in your class. If you do so, we recommend that you follow the built-in semantics described below.</p>

<dl>

<dt id="isa($class)"><a class="permalink" href="perlobj.html#isa($class)">#</a><a id="isa"></a><a id="isa-class"></a>isa($class) </dt>
<dd>

<p>The <code>isa</code> method returns <i>true</i> if the object is a member of the class in <code>$class</code>, or a member of a subclass of <code>$class</code>.</p>

<p>If you override this method, it should never throw an exception.</p>

</dd>
<dt id="DOES($role)"><a class="permalink" href="perlobj.html#DOES($role)">#</a><a id="DOES"></a><a id="DOES-role"></a>DOES($role) </dt>
<dd>

<p>The <code>DOES</code> method returns <i>true</i> if its object claims to perform the role <code>$role</code>. By default, this is equivalent to <code>isa</code>. This method is provided for use by object system extensions that implement roles, like <code>Moose</code> and <code>Role::Tiny</code>.</p>

<p>You can also override <code>DOES</code> directly in your own classes. If you override this method, it should never throw an exception.</p>

</dd>
<dt id="can($method)"><a class="permalink" href="perlobj.html#can($method)">#</a><a id="can"></a><a id="can-method"></a>can($method) </dt>
<dd>

<p>The <code>can</code> method checks to see if the class or object it was called on has a method named <code>$method</code>. This checks for the method in the class and all of its parents. If the method exists, then a reference to the subroutine is returned. If it does not then <code>undef</code> is returned.</p>

<p>If your class responds to method calls via <code>AUTOLOAD</code>, you may want to overload <code>can</code> to return a subroutine reference for methods which your <code>AUTOLOAD</code> method handles.</p>

<p>If you override this method, it should never throw an exception.</p>

</dd>
<dt id="VERSION($need)"><a class="permalink" href="perlobj.html#VERSION($need)">#</a><a id="VERSION"></a><a id="VERSION-need"></a>VERSION($need) </dt>
<dd>

<p>The <code>VERSION</code> method returns the version number of the class (package).</p>

<p>If the <code>$need</code> argument is given then it will check that the current version (as defined by the $VERSION variable in the package) is greater than or equal to <code>$need</code>; it will die if this is not the case. This method is called automatically by the <code>VERSION</code> form of <code>use</code>.</p>

<pre><code>use Package 1.2 qw(some imported subs);
# implies:
Package-&gt;VERSION(1.2);</code></pre>

<p>We recommend that you use this method to access another package&#39;s version, rather than looking directly at <code>$Package::VERSION</code>. The package you are looking at could have overridden the <code>VERSION</code> method.</p>

<p>We also recommend using this method to check whether a module has a sufficient version. The internal implementation uses the <a href="version.html">version</a> module to make sure that different types of version numbers are compared correctly.</p>

</dd>
</dl>

<h2 id="AUTOLOAD"><a class="permalink" href="perlobj.html#AUTOLOAD">#</a>AUTOLOAD </h2>

<p>If you call a method that doesn&#39;t exist in a class, Perl will throw an error. However, if that class or any of its parent classes defines an <code>AUTOLOAD</code> method, that <code>AUTOLOAD</code> method is called instead.</p>

<p><code>AUTOLOAD</code> is called as a regular method, and the caller will not know the difference. Whatever value your <code>AUTOLOAD</code> method returns is returned to the caller.</p>

<p>The fully qualified method name that was called is available in the <code>$AUTOLOAD</code> package global for your class. Since this is a global, if you want to refer to do it without a package name prefix under <code>strict &#39;vars&#39;</code>, you need to declare it.</p>

<pre><code># XXX - this is a terrible way to implement accessors, but it makes
# for a simple example.
our $AUTOLOAD;
sub AUTOLOAD {
    my $self = shift;

    # Remove qualifier from original method name...
    my $called =  $AUTOLOAD =~ s/.*:://r;

    # Is there an attribute of that name?
    die &quot;No such attribute: $called&quot;
        unless exists $self-&gt;{$called};

    # If so, return it...
    return $self-&gt;{$called};
}

sub DESTROY { } # see below</code></pre>

<p>Without the <code>our $AUTOLOAD</code> declaration, this code will not compile under the <a href="strict.html">strict</a> pragma.</p>

<p>As the comment says, this is not a good way to implement accessors. It&#39;s slow and too clever by far. However, you may see this as a way to provide accessors in older Perl code. See <a href="perlootut.html">perlootut</a> for recommendations on OO coding in Perl.</p>

<p>If your class does have an <code>AUTOLOAD</code> method, we strongly recommend that you override <code>can</code> in your class as well. Your overridden <code>can</code> method should return a subroutine reference for any method that your <code>AUTOLOAD</code> responds to.</p>

<h2 id="Destructors"><a class="permalink" href="perlobj.html#Destructors">#</a>Destructors  </h2>

<p>When the last reference to an object goes away, the object is destroyed. If you only have one reference to an object stored in a lexical scalar, the object is destroyed when that scalar goes out of scope. If you store the object in a package global, that object may not go out of scope until the program exits.</p>

<p>If you want to do something when the object is destroyed, you can define a <code>DESTROY</code> method in your class. This method will always be called by Perl at the appropriate time, unless the method is empty.</p>

<p>This is called just like any other method, with the object as the first argument. It does not receive any additional arguments. However, the <code>$_[0]</code> variable will be read-only in the destructor, so you cannot assign a value to it.</p>

<p>If your <code>DESTROY</code> method throws an exception, this will not cause any control transfer beyond exiting the method. The exception will be reported to <code>STDERR</code> as a warning, marked &quot;(in cleanup)&quot;, and Perl will continue with whatever it was doing before.</p>

<p>Because <code>DESTROY</code> methods can be called at any time, you should localize any global status variables that might be set by anything you do in your <code>DESTROY</code> method. If you are in doubt about a particular status variable, it doesn&#39;t hurt to localize it. There are five global status variables, and the safest way is to localize all five of them:</p>

<pre><code>sub DESTROY {
    local($., $@, $!, $^E, $?);
    my $self = shift;
    ...;
}</code></pre>

<p>If you define an <code>AUTOLOAD</code> in your class, then Perl will call your <code>AUTOLOAD</code> to handle the <code>DESTROY</code> method. You can prevent this by defining an empty <code>DESTROY</code>, like we did in the autoloading example. You can also check the value of <code>$AUTOLOAD</code> and return without doing anything when called to handle <code>DESTROY</code>.</p>

<h3 id="Global-Destruction"><a class="permalink" href="perlobj.html#Global-Destruction">#</a><a id="Global"></a>Global Destruction</h3>

<p>The order in which objects are destroyed during the global destruction before the program exits is unpredictable. This means that any objects contained by your object may already have been destroyed. You should check that a contained object is defined before calling a method on it:</p>

<pre><code>sub DESTROY {
    my $self = shift;

    $self-&gt;{handle}-&gt;close() if $self-&gt;{handle};
}</code></pre>

<p>You can use the <code>${^GLOBAL_PHASE}</code> variable to detect if you are currently in the global destruction phase:</p>

<pre><code>sub DESTROY {
    my $self = shift;

    return if ${^GLOBAL_PHASE} eq &#39;DESTRUCT&#39;;

    $self-&gt;{handle}-&gt;close();
}</code></pre>

<p>Note that this variable was added in Perl 5.14.0. If you want to detect the global destruction phase on older versions of Perl, you can use the <code>Devel::GlobalDestruction</code> module on CPAN.</p>

<p>If your <code>DESTROY</code> method issues a warning during global destruction, the Perl interpreter will append the string &quot; during global destruction&quot; to the warning.</p>

<p>During global destruction, Perl will always garbage collect objects before unblessed references. See <a href="perlhacktips.html#PERL_DESTRUCT_LEVEL">&quot;PERL_DESTRUCT_LEVEL&quot; in perlhacktips</a> for more information about global destruction.</p>

<h2 id="Non-Hash-Objects"><a class="permalink" href="perlobj.html#Non-Hash-Objects">#</a><a id="Non"></a>Non-Hash Objects</h2>

<p>All the examples so far have shown objects based on a blessed hash. However, it&#39;s possible to bless any type of data structure or referent, including scalars, globs, and subroutines. You may see this sort of thing when looking at code in the wild.</p>

<p>Here&#39;s an example of a module as a blessed scalar:</p>

<pre><code>package Time;

use strict;
use warnings;

sub new {
    my $class = shift;

    my $time = time;
    return bless \$time, $class;
}

sub epoch {
    my $self = shift;
    return $$self;
}

my $time = Time-&gt;new();
print $time-&gt;epoch();</code></pre>

<h2 id="Inside-Out-objects"><a class="permalink" href="perlobj.html#Inside-Out-objects">#</a><a id="Inside"></a>Inside-Out objects</h2>

<p>In the past, the Perl community experimented with a technique called &quot;inside-out objects&quot;. An inside-out object stores its data outside of the object&#39;s reference, indexed on a unique property of the object, such as its memory address, rather than in the object itself. This has the advantage of enforcing the encapsulation of object attributes, since their data is not stored in the object itself.</p>

<p>This technique was popular for a while (and was recommended in Damian Conway&#39;s <i>Perl Best Practices</i>), but never achieved universal adoption. The <a href="./Object::InsideOut.html">Object::InsideOut</a> module on CPAN provides a comprehensive implementation of this technique, and you may see it or other inside-out modules in the wild.</p>

<p>Here is a simple example of the technique, using the <a href="./Hash::Util::FieldHash.html">Hash::Util::FieldHash</a> core module. This module was added to the core to support inside-out object implementations.</p>

<pre><code>package Time;

use strict;
use warnings;

use Hash::Util::FieldHash &#39;fieldhash&#39;;

fieldhash my %time_for;

sub new {
    my $class = shift;

    my $self = bless \( my $object ), $class;

    $time_for{$self} = time;

    return $self;
}

sub epoch {
    my $self = shift;

    return $time_for{$self};
}

my $time = Time-&gt;new;
print $time-&gt;epoch;</code></pre>

<h2 id="Pseudo-hashes"><a class="permalink" href="perlobj.html#Pseudo-hashes">#</a><a id="Pseudo"></a>Pseudo-hashes</h2>

<p>The pseudo-hash feature was an experimental feature introduced in earlier versions of Perl and removed in 5.10.0. A pseudo-hash is an array reference which can be accessed using named keys like a hash. You may run in to some code in the wild which uses it. See the <a href="fields.html">fields</a> pragma for more information.</p>

<h1 id="SEE-ALSO"><a class="permalink" href="perlobj.html#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1>

<p>A kinder, gentler tutorial on object-oriented programming in Perl can be found in <a href="perlootut.html">perlootut</a>. You should also check out <a href="perlmodlib.html">perlmodlib</a> for some style guides on constructing both modules and classes.</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
